// Filename: router.js
define([
  'jquery',
  'underscore',
  'backbone',
  'views/feeds/FeedsView',
  'views/settings/SettingView',
  'views/search/SearchView',
  'views/sidebar/SidebarView',
  'views/search/SearchResultsView',
  'views/appointments/TakeAppointmentView',
  'views/schedule/ScheduleView',
  'views/appointments/DoctorView',
  'views/appointments/DoctorReadOnlyView',
  'views/charts/DoctorChartsView',
 // 'views/grids/DoctorGridView',
  'views/search/HistoryView',
  'views/search/HistoryResultsView',
  'views/admin/UnattendedView',
  'views/admin/AdminGraphView',
  'views/admin/ApproveUsersView',
  'views/videosession/VideoSessionView',
  'views/history/PatientHistoryView',
  'views/lab/LabTestView',
  'views/profiles/ProfileView',
  'views/refer/ReferPatientView',
  'views/response/ManageResponseView',
], function($, _, Backbone, FeedsView,SettingView,SearchView,SidebarView,SearchResultsView,TakeAppointmentView,ScheduleView,DoctorView,
		DoctorReadOnlyView,DoctorChartsView,/*DoctorGridView,*/HistoryView,HistoryResultsView,UnattendedView, AdminGraphView,ApproveUsersView,
		VideoSessionView
		,PatientHistoryView,LabTestView,ProfileView,ReferPatientView,ManageResponseView
		) {
  
  var AppRouter = Backbone.Router.extend({
	  initialize: function(el) {
			this.el = el;
			
			this.sidebarView = new SidebarView();
			this.feedsView = new FeedsView();
			this.settingView = new SettingView();
			this.searchView = new SearchView();
			this.searchResults=new SearchResultsView();
			this.takeAppointmentView = new TakeAppointmentView();
			this.scheduleView=new ScheduleView();
			this.doctorView=new DoctorView();
			this.doctorReadOnlyView=new DoctorReadOnlyView();
			this.doctorChartsView=new DoctorChartsView();
		//	this.doctorGridView = new DoctorGridView();
			this.historyView = new HistoryView();
			this.historyResults=new HistoryResultsView();
			this.unattendedView=new UnattendedView();
			this.adminGraphView=new AdminGraphView();
			this.approveUsersView=new ApproveUsersView();
			this.videoSessionView= new VideoSessionView();
			this.patientHistoryView = new PatientHistoryView();
			this.labTestView = new LabTestView();
			this.profileView = new ProfileView();
			this.referPatientView= new ReferPatientView();
			this.manageResponseView = new ManageResponseView();
		},
    routes: {
      // Define some URL routes
  //    'projects': 'showProjects',
      'activity': 'showActivities',
      'usersettings': 'showSettings',
      'search': 'searchDoctor',
      'searchresults': 'searchResults',
      'takeappointment': 'takeAppointment',
      'schedule': 'schedule',
      'docappointment': 'showdocappointment',
      'docrdappointment': 'showrddocappointment',
      'doccharts': 'showdoccharts',
      'docmonfinance':'showdocmonfinance',
      'history': 'historyPatient',
      'historyresults': 'historyResults',
      'newappointments': 'unattended',   
      'admingraphs': 'showGraphs',   
      'approveusers':'approveusers',
      'videosession':'startSession',
      'patientHistory':'showPatientHistory',
      'labtest':'showLabTest',
      'addProfiles':'showAddProfiles',
      'referPatient':'showReferPatient',
      'manageResponse':'showResponseTemplates',
      // Default
      '*default': 'decideAction'
    }
  });
  
  var initialize = function(){

    var app_router = new AppRouter;
    
    app_router.on('route:showActivities', function(){
    	$(".mainpanel").html("Loading...");
    	//event.preventDefault();
 	//	var sidebarView = new SidebarView();
		this.sidebarView.render();
        //var feedsView = new FeedsView();
		this.feedsView.render(this.sidebarView);
		return false;

    });
    app_router.on('route:showSettings', function(){
    	$(".mainpanel").html("Loading...");
    	 //event.preventDefault();
 		//var sidebarView = new SidebarView();
    	 this.sidebarView.render();
    	//var settingView = new SettingView();	
    	 this.settingView.render();
    	 return false;

    });
    app_router.on('route:searchDoctor', function(){
    	$(".mainpanel").html("Loading...");
    	//event.preventDefault();
		//var sidebarView = new SidebarView();
    	this.sidebarView.render();
   	 //var searchView = new SearchView();	
    	this.searchView.render();
    	if(this.searchView.model.get("searchresults")){
        	var searchResultsModel = Backbone.Model.extend({});
        	var searchResultList = Backbone.Collection.extend({model: searchResultsModel});
        	var searchModelList = new searchResultList(this.searchView.model.get("searchresults"));
        	this.searchResults = new SearchResultsView({collection: searchModelList});
        	this.searchResults.render();
    	}
    	return false;
   });

    app_router.on('route:searchResults', function(){
    //	$(".mainpanel").html("Loading...");
    	//event.preventDefault();
    	if(this.searchView.model.get("searchresults")){
           	var searchResultsModel = Backbone.Model.extend({});
        	var searchResultList = Backbone.Collection.extend({model: searchResultsModel});
        	var searchModelList = new searchResultList(this.searchView.model.get("searchresults"));
        	this.searchResults = new SearchResultsView({collection: searchModelList});
        	this.searchResults.render();
    		
    	}else{
    		this.navigate("history", true);
    	}
    	return false;
    });
    
    app_router.on('route:historyPatient', function(){
    	$(".mainpanel").html("Loading...");
    	//event.preventDefault();
		//var sidebarView = new SidebarView();
    	this.sidebarView.render();
   	 //var searchView = new SearchView();	
    	this.historyView.render();
    	if(this.historyView.model.get("historyresults")){
        	var historyResultsModel = Backbone.Model.extend({});
        	var historyResultList = Backbone.Collection.extend({model: historyResultsModel});
        	var historyModelList = new historyResultList(this.historyView.model.get("historyresults"));
        	this.historyResults = new HistoryResultsView({collection: historyModelList});
        	this.historyResults.render();
    	}
    	return false;
   });

    app_router.on('route:historyResults', function(){
    //	$(".mainpanel").html("Loading...");
    	//event.preventDefault();
    	if(this.historyView.model.get("historyresults")){
           	var historyResultsModel = Backbone.Model.extend({});
        	var historyResultList = Backbone.Collection.extend({model: historyResultsModel});
        	var historyModelList = new historyResultList(this.historyView.model.get("historyresults"));
        	this.historyResults = new HistoryResultsView({collection: historyModelList});
        	this.historyResults.render();
    		
    	}else{
    		this.navigate("history", true);
    	}
    	return false;
    });
    
    app_router.on('route:showPatientHistory', function(){
    	$(".mainpanel").html("Loading...");
    	//event.preventDefault();
		//var sidebarView = new SidebarView();
    	this.sidebarView.render();
   	 //var searchView = new SearchView();	
    	this.patientHistoryView.render();
    	return false;
   });

    
    
    app_router.on('route:takeAppointment', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		if(this.searchResults.doctorId){
			this.takeAppointmentView.render(this.searchResults.doctorId,this.searchResults.hospitalId);
		}else{
			this.navigate("search", true);
		}
		return false;
    });
    
    app_router.on('route:showdocappointment', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		if(this.feedsView.selectedappointment){
			this.doctorView.render(this.feedsView.selectedappointment);
		}else{
			this.navigate("activity", true);
		}
		return false;
    });
    app_router.on('route:showrddocappointment', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		if(this.feedsView.selectedappointment){
			this.doctorReadOnlyView.render(this.feedsView.selectedappointment);
		}else{
			this.navigate("activity", true);
		}
		return false;
    });
    app_router.on('route:schedule', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.scheduleView.render();
		return false;
    });
    
    app_router.on('route:showdoccharts', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.doctorChartsView.render();
		return false;
    });
    
    app_router.on('route:unattended', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.unattendedView.render();
		return false;
    });
    
    app_router.on('route:showGraphs', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.adminGraphView.render();
		return false;
    });
    app_router.on('route:showLabTest', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		if(this.feedsView.selectedappointment){
			this.labTestView.render(this.feedsView.selectedappointment);
		}else{
			this.navigate("activity", true);
		}
		return false;
    });

    
    app_router.on('route:showAddProfiles', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.profileView.render();
		return false;
    });
    
    app_router.on('route:showReferPatient', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.referPatientView.render();
		return false;
    });

    app_router.on('route:showResponseTemplates', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.manageResponseView.render();
		return false;
    });

        
    app_router.on('route:approveusers', function(){
    	$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		this.approveUsersView.render();
		return false;
    });

    app_router.on('route:startSession', function(){
    	//$(".mainpanel").html("");
    	//event.preventDefault();
		this.sidebarView.render();
		var appointment = this.doctorView.appointment?this.doctorView.appointment:(this.feedsView.appointment?this.feedsView.appointment:null);
		if(appointment){
			this.videoSessionView.render(appointment);
		}else{
			this.navigate("activity", true);
		}
		return false;
		
    });

    
    app_router.on('route:decideAction', function(){
		 $.ajax({
		        type: "GET",
		        url: "/settingDetails",
		        success: function(data){
		        	if(!data.user.userType){
		        		this.settingView= new SettingView();	
		        		this.settingView.render();
		        		
		        	}else{
		        		data.user.userType != "A"?
		        				(data.user.userType != "S"?app_router.navigate("activity", true):app_router.navigate("admingraphs", true))		        				
		        				: app_router.navigate("newappointments", true);
		        	}
		        }
	    });
    });
    Backbone.View.prototype.fadeIn = function(template, wrapper) {
        wrapper.is(':hidden') ? 
        wrapper.html(template).show(300) : 
        wrapper.hide(300, function(){ wrapper.html(template).show(300) });
    };
    $('.loader')
    //.hide()  // hide it initially
    .ajaxStart(function() {
    	//$(this).dialog({height:30,width:30});
       // $(this).show();
    	  var cloned = $(this)
       //   .clone()
          .addClass('wmodal')
          .css('top', $(this).offset().top)
          .css('left', $(this).offset().left); 
      $('body').append(cloned);
      $('#grayout').css('height', $(document).height()).css('width', $(document).width()).fadeIn();
    })
    .ajaxStop(function() {
    	//$(this).dialog("close");
    	 $('#grayout').fadeOut();
    	    $(".wmodal").removeClass("wmodal");
    	    //$(this).hide();
    })
;
    Backbone.View.prototype.goTo = function (loc) {
    	app_router.navigate();
    	app_router.navigate(loc, true);
      };
      Backbone.View.prototype.getRouter= app_router;

     Backbone.history.start();

  };
  return { 
    initialize: initialize
  };
});
